package com.ws.hzuoj.admin.user.role.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ws.common.easyui.util.Page;
import com.ws.common.easyui.util.PageUtils;
import com.ws.user.role.entity.Role;
import com.ws.user.role.entity.RolePermission;
import com.ws.user.role.service.IRolePermissionService;
import com.ws.user.role.service.IRoleService;
import com.ws.user.user.entity.UserRole;
import com.ws.user.user.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/user/role")
@Controller
public class RoleWeb {


    @Autowired
    IRoleService roleService;
    @Autowired
    IUserRoleService userRoleService;
    @Autowired
    IRolePermissionService rolePermissionService;

    @RequestMapping("{type}")
    public String list(@PathVariable String type){
        return  null;
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(Role role,Page page){
        List<Role> roles=roleService.list(role,page);
        return PageUtils.getEasyUiList(roles);
     }


    @RequestMapping("/rolePermissionSave")
    @ResponseBody
    public Object rolePermissionSave(String roleId,String[] permissionIds){
        RolePermission rolePermission=new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermissionService.deleteByRoleId(roleId);
        for(String id :permissionIds){
            rolePermission.setPermissionId(id);
            rolePermission.setId(null);
            rolePermissionService.insert(rolePermission);
        }
        return "";
    }

    @RequestMapping("/listByUserId")
    @ResponseBody
    public Object listByUserId(String userId){
        List<Role> roles=roleService.listAll();

        List<String> checkedKeys=userRoleService.listRoleByUserId(userId);

        Map<String,Object> map=new HashMap<>();
        map.put("roles",roles);
        map.put("checkedKeys",checkedKeys);
        return map;
    }

    @RequestMapping("read")
    @ResponseBody
    public Object read(Role role){
        if(StrUtil.isNotEmpty(role.getId())){
            role= roleService.read(role.getId());
        }
        return  role;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(Role role){
        Integer count=roleService.save(role);
        return  count;
     }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(String id){
        return  roleService.delete(id);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Role role){
        Integer count=roleService.insert(role);
        return  count;
    }

    @RequestMapping("/insertSelective")
    @ResponseBody
    public Object insertSelective(Role role){
        return roleService.insertSelective(role);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(Role role){
        return roleService.update(role);
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public Object listByMap(){
        return  roleService.listAll();
    }
}
